package com.yhgm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhgm.common.BaseResponse;
import com.yhgm.common.ErrorCode;
import com.yhgm.common.ResultUtils;
import com.yhgm.data.ReqCopy;
import com.yhgm.exception.BusinessException;
import com.yhgm.mapper.ImageMapper;
import com.yhgm.pojo.Image;
import com.yhgm.service.ImageService;
import com.yhgm.utils.AliOssUtil;
import io.swagger.models.auth.In;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.logging.Filter;

/**
 * @Author ZY
 * @Date 2024/4/26 14:05
 */
@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image> implements ImageService {
    @Resource
    private ImageMapper imageMapper;
    @Resource
    private AliOssUtil aliOssUtil;
    @Override
    public BaseResponse<Integer> addFile(Image image) {
        Image image1 = new Image();
        image1.setPath(image.getPath());
        image1.setFileName(image.getFileName());
        List<Image> image2 = imageMapper.findByCondition(image1);
        if (image2.size() > 0){
            throw new BusinessException(ErrorCode.CHRISTEN);
        }
        Integer i = (Integer)imageMapper.insert(image);
        if (i <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(i);
    }

    @Override
    public BaseResponse<List<Image>> listFile(String url) {
        List<Image> images = imageMapper.listFile(url);
        return ResultUtils.success(images);
    }

    @Override
    public BaseResponse<Long> delFile(Long[] ids) {
        for (Long id : ids){
            Image image1 = imageMapper.selectById(id);
            Integer classType = image1.getClassType();
            if (classType == 1){
                imageMapper.delFile(id);
                Integer i = 1; //未进入回收站
                String path;
                if (image1.getPath().length() <= 1){
                    path = image1.getPath()+image1.getFileName();
                }else{
                    path = image1.getPath()+"/"+image1.getFileName();
                }
                List<Image> images = imageMapper.findByPath(path,i);
                for (Image image:images) {
                    imageMapper.delFile(image.getId());
                }
            }else{
                Integer i = imageMapper.delFile(id);
            }

        }
        return ResultUtils.success(1L);
    }

    @Override
    public BaseResponse<Integer> updateFile(Image image) {
        Image image1 = new Image();
        image1.setPath(image.getPath());
        image1.setFileName(image.getFileName());
        List<Image> byCondition = imageMapper.findByCondition(image1);
        if (byCondition.size()>0){
            throw new BusinessException(ErrorCode.CHRISTEN);
        }

        Integer i = imageMapper.updateById(image);
        if (i <= 0) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(1);
    }

    @Override
    public BaseResponse<List<Image>> listRecycle(String path) {
        List<Image> images = imageMapper.listRecycle(path);
        return ResultUtils.success(images);
    }

    @Override
    public BaseResponse<Integer> recoverImages(Long[] ids) {
        for (Long id: ids) {
            Image image = imageMapper.selectById(id);
            Integer classType = image.getClassType();
            if(classType == 1){
                imageMapper.recoverImage(id);
                String path;
                if (image.getPath().length() <= 1){
                    path = image.getPath()+image.getFileName();
                }else{
                    path = image.getPath()+"/"+image.getFileName();
                }
                List<Image> byPath = imageMapper.findByPath(path, 2);
                for (Image image1:byPath) {
                    imageMapper.recoverImage(image1.getId());
                }
            }else {
                imageMapper.recoverImage(id);
            }
        }
        return ResultUtils.success(1);
    }

    @Override
    public BaseResponse<Long> delRecycle(Long[] ids) {
        for (Long id: ids) {
            Image image = imageMapper.selectById(id);
            Integer classType = image.getClassType();
            if(classType == 1){
                imageMapper.delRecycle(id);
                String path;
                if (image.getPath().length() <= 1){
                    path = image.getPath()+image.getFileName();
                }else{
                    path = image.getPath()+"/"+image.getFileName();
                }
                List<Image> byPath = imageMapper.findByPath(path, 2);
                for (Image image1:byPath) {
                    imageMapper.delRecycle(image1.getId());
                }
            }else {
                imageMapper.delRecycle(id);
            }
        }
        return ResultUtils.success(1L);
    }

    @Override
    public BaseResponse<List<Image>> findByCondition(Image image) {
        List<Image> images = imageMapper.findByCondition(image);
        return ResultUtils.success(images);
    }

    @Override
    public BaseResponse<Long> copyFile(ReqCopy reqCopy) throws Exception {
        for (Long id:reqCopy.getIds()) {
            Image image = imageMapper.selectById(id);
            image.setPath(reqCopy.getDestinationAddress());
            copy(image);
            if (image.getClassType() == 1){
                String sourceAddress = reqCopy.getSourceAddress();
                String destinationAddress = reqCopy.getDestinationAddress();
                if (sourceAddress.length()>=2){
                    sourceAddress = sourceAddress + "/";
                }
                if (destinationAddress.length()>=2){
                    destinationAddress = destinationAddress + "/";
                }
               findFile(sourceAddress+image.getFileName(),destinationAddress+image.getFileName());
            }
        }
        return ResultUtils.success(1L);
    }

    @Override
    public String uploadFile(MultipartFile file) throws IOException {
        return aliOssUtil.upload(file);
    }

    private void  findFile(String sourceAddress,String destinationAddress){
        List<Image> images = imageMapper.findByPath(sourceAddress, 1);
        for (Image s:images) {
            Image image1 = new Image();
            image1 = s;
            s.setPath(destinationAddress);
            copy(s);
            if (s.getClassType() == 1){
                if (sourceAddress.length()>=2){
                    sourceAddress = sourceAddress + "/";
                }
                if (destinationAddress.length()>=2){
                    destinationAddress = destinationAddress + "/";
                }
                findFile(sourceAddress+s.getFileName(),destinationAddress+s.getFileName());
            }
        }
    }

    private void copy(Image image){
        image.setId(0L);
        addFile(image);
    }
}
